// 请求配置
import axios from 'axios';
import { message } from 'antd';
import { history } from 'umi';
const baseURL: string = 'http://localhost:2000/';

// axios 的实例及拦截器配置
const axiosInstance = axios.create({
  baseURL,
});

// 请求拦截
axiosInstance.interceptors.request.use(function (config) {
  let token = localStorage.getItem('token'); // 获取我们存储的 token
  config.headers['Authorization'] = 'Bearer ' + token; // 将 token 放到 header 里面
  config.timeout = 60000;
  return config;
});

// 响应拦截
axiosInstance.interceptors.response.use(
  res => res.data,
  err => {
    // token失效
    if (err.response.status === 302) {
      localStorage.removeItem('token');
      history.push('/login');
    }

    message.info(err.response.data.message);
    return Promise.reject(err);
  }
);

export { baseURL, axiosInstance };
